package com.itheima.d8_thread_pool;

import java.util.List;
import java.util.concurrent.*;

/*
     目标：线程池处理任务
     需求 : 使用线程池处理Callable类型任务
 */
public class ThreadPoolTest3 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ThreadPoolExecutor threadPool = new ThreadPoolExecutor(
                3,
                10,
                60,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(20),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.DiscardOldestPolicy()
        );
        Future<Integer> submit = threadPool.submit(new myCallable(100));
        Future<Integer> submit1 = threadPool.submit(new myCallable(50));
        Future<Integer> submit2 = threadPool.submit(new myCallable(10));

        Integer i = submit.get();
        System.out.println(i);

        List<Runnable> runnables = threadPool.shutdownNow();
        System.out.println(runnables);

        i = submit1.get();
        System.out.println(i);

        i = submit2.get();
        System.out.println(i);

        threadPool.shutdown();
    }
}

class myCallable implements Callable<Integer>{
    private int num;
    myCallable(int num){
        this.num=num;
    }
    @Override
    public Integer call() throws Exception {
        int sum=0;
        for (int i = 0; i < num; i++) {
            sum+=i;
        }
        return sum;
    }
}